home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Graphics Samples / ShapePart Browser ƒ / ShapeSetup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-15  |  8.4 KB  |  331 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    ShapeSetup.c
  3.  *
  4.  *    Robert Dierkes,  April 26, 1993 
  5.  */
  6.  
  7. #include "graphics routines.h"
  8. #include "math routines.h"
  9. #include "graphics libraries.h"
  10. #include "font library.h"
  11.  
  12. #include "ShapeSetup.h"
  13.  
  14.  
  15. /*----------------------*/
  16. /*    Global Declarations    */
  17. /*----------------------*/
  18.  
  19.  
  20. /*------------------------------*/
  21. /*    External Declarations     */
  22. /*------------------------------*/
  23.  
  24.  
  25. /*------------------------------*/
  26. /*       Local ProtoTypes       */
  27. /*------------------------------*/
  28.  
  29.  
  30.     Boolean
  31. CreateShapesFrame (WindowPtr pWindow, register long numRows, register long numCols, gxShape *pBoxSet)
  32. {
  33.     #define        kSixteenthInch    (IntToFixed (4) + kFixOneHalf)
  34.  
  35.     gxRectangle        bounds;
  36.     register
  37.     long            r, c;
  38.     gxShape            box;
  39.     fixed            side,
  40.                     left,
  41.                     top;
  42.  
  43.     if (numRows == 0  ||  numCols == 0)
  44.     {
  45.         DebugStr ("\pCreateShapesFrame: Number of rows or columns is zero");
  46.         return (false);
  47.     }
  48.  
  49.     /* Find side of each enclosing box */
  50.     side = FixedDivide (IntToFixed (pWindow->portRect.bottom - pWindow->portRect.top), IntToFixed(numCols)) - kSixteenthInch;
  51.  
  52.     *pBoxSet = GXNewShape (gxEmptyType);
  53.     for (r = 0, top = kSixteenthInch + (kSixteenthInch>>1); r < numRows; r++, top += side)
  54.     {
  55.         for (c = 0, left = kSixteenthInch + (kSixteenthInch>>1); c < numCols; c++, left += side)
  56.         {
  57.             box = NewShape4 (gxRectangleType, left, top, left + side, top + side);
  58.             GXInsetShape (box, kSixteenthInch);
  59.             GXSetShapeFill (box, gxEvenOddFill);
  60.             AddToShape (*pBoxSet, box);
  61.             GXDisposeShape (box);
  62.         }
  63.     }
  64.     SetShapeCommonColor (*pBoxSet, grayish + silver);
  65.  
  66.     return (true);
  67. }
  68.  
  69.  
  70.     Boolean
  71. CreateHitTestShapes (WindowPtr pWindow, gxShape **h1stHitShape, gxShape *pBoxSet, register long shapeCount)
  72. {
  73.     gxShape            *pShape;
  74.     register
  75.     long            shapeNum;
  76.     gxRectangle        bounds;
  77.  
  78.     if (shapeCount <= 0)
  79.     {
  80.         DebugStr ("\pCreateHitTestShapes: Number of shapes is <= zero");
  81.         return (false);
  82.     }
  83.  
  84.     if (h1stHitShape == nil)
  85.     {
  86.         DebugStr ("\pCreateHitTestShapes: Shape list handle is nil");
  87.         return (false);
  88.     }
  89.  
  90.     /* Allocate space for hit shapes */
  91.     *h1stHitShape = pShape = (gxShape *) NewPtr (shapeCount * sizeof (gxShape));
  92.     if (*h1stHitShape == nil)
  93.     {
  94.         DebugStr ("\pCreateHitTestShapes: NewPtr for shapes failed");
  95.         return (false);
  96.     }
  97.  
  98.     for (shapeNum = 0; shapeNum < shapeCount; shapeNum++, pShape++)
  99.     {
  100.         switch (shapeNum)
  101.         {
  102.         case kTrapizoidPolygon:
  103.             *pShape = NewShape4 (gxRectangleType, 0, 0, kHalfInch, kHalfInch);
  104.             SkewShapeAboutCenter (*pShape, -kFixOneHalf, 0);
  105.             SetShapeCommonColor (*pShape, light + blue);
  106.             break;
  107.  
  108.         case kOvalPath:
  109.             bounds.left        =
  110.             bounds.top        = 0;
  111.             bounds.right    =
  112.             bounds.bottom    = kHalfInch;
  113.             *pShape = NewOval (&bounds);
  114.  
  115.             GXSetShapeFill (*pShape, gxClosedFrameFill);
  116.             GXSetShapePen (*pShape, IntToFixed (15));
  117.             SetShapeCommonColor (*pShape, rose_madder);
  118.             break;
  119.  
  120.         case kArrowPolygon:
  121.             {
  122.                 fixed    arrow[] = {1L, 7L,    0,IntToFixed (5),
  123.                                             0, IntToFixed (15),
  124.                                             IntToFixed (12), IntToFixed (14),
  125.                                             IntToFixed (10), IntToFixed (20),
  126.                                             IntToFixed (27), IntToFixed (10),
  127.                                             IntToFixed (10), 0,
  128.                                             IntToFixed (12), IntToFixed (6)};
  129.                 gxJoinRecord    joinRec;
  130.  
  131.                 *pShape    = GXNewPolygons ((gxPolygons *) arrow);
  132.  
  133.                 joinRec.attributes = gxCurveJoin;
  134.                 joinRec.join = nil;
  135.                 joinRec.miter = 0;
  136.                 GXSetShapeJoin (*pShape, &joinRec);
  137.                 GXSetShapeFill (*pShape, gxClosedFrameFill);
  138.                 GXSetShapePen (*pShape, IntToFixed (12));
  139.  
  140.                 RotateShapeAboutCenter (*pShape, IntToFixed (-45));
  141.                 SetShapeCommonColor (*pShape, turquoise);
  142.             }
  143.             break;
  144.  
  145.         case kPatternedPolygon:
  146.             {
  147.                 fixed    star[] = {1L, 5L,IntToFixed (3), IntToFixed (18),
  148.                                         IntToFixed (9),    0,
  149.                                         IntToFixed (15),IntToFixed (18),
  150.                                         0,IntToFixed (6),
  151.                                         IntToFixed (18), IntToFixed (6)};
  152.                 gxPatternRecord    patternRec;
  153.  
  154.                 *pShape = NewShape4 (gxRectangleType, 0, 0, kHalfInch, kHalfInch);
  155.                 RotateShapeAboutCenter (*pShape, IntToFixed (-6));
  156.                 SetShapeCommonColor (*pShape, gxWhite);
  157.  
  158.                 patternRec.pattern = GXNewPolygons ((gxPolygons *) star);
  159.                 GXSetShapeFill (patternRec.pattern, gxWindingFill);
  160.                 patternRec.u.x =  kQuarterInch;
  161.                 patternRec.u.y = -kQuarterInch;
  162.                 patternRec.v.x =  kQuarterInch;
  163.                 patternRec.v.y =  kEigthInch;
  164.                 GXSetShapePattern (*pShape, &patternRec);
  165.                 GXDisposeShape (patternRec.pattern);
  166.             }
  167.             break;
  168.  
  169.         case kWordGlyphs:
  170.             {
  171.                 #define    kText    "GX"
  172.                 gxPoint    position = {0, 0};
  173.  
  174.                 *pShape = NewCString (kText, &position);
  175.                 GXSetShapeType (*pShape, gxGlyphType);
  176.                 GXSetShapeTextSize (*pShape, IntToFixed (66));
  177.                 SetStyleCNamedFont (GXGetShapeStyle (*pShape), "Hoefler Italic");
  178.                 SetShapeCommonColor (*pShape, indigo);
  179.             }
  180.             break;
  181.  
  182.         case kArchCurve:
  183.             {
  184.                 fixed    arch[] = {    0,                kHalfInch,
  185.                                     kEigthInch,        0,
  186.                                     kQuarterInch,    kHalfInch};
  187.  
  188.                 *pShape = GXNewCurve ((gxCurve *) arch);
  189.                 GXSetShapePen (*pShape, IntToFixed (20));
  190.                 SetShapeCommonColor (*pShape, light + maroon);    /* light + violet */
  191.                 ScaleShapeAboutCenter (*pShape, 5 * fixed1, 3 * fixed1);
  192.             }
  193.             break;
  194.  
  195.         case kCapsLine:
  196.             {
  197.                 #define    kFixOneQuarter    (fixed1 >> 2)
  198.                 #define    kFix3Quarters    ((3 * fixed1) >> 2)
  199.  
  200.                 fixed    star[] = {1L, 5L,    -fixed1-kFixOneQuarter,    -3*fixed1+kFixOneQuarter,
  201.                                             -fixed1-kFixOneQuarter,     2*fixed1+kFixOneHalf,
  202.                                              fixed1+kFix3Quarters,    -2*fixed1,
  203.                                             -3*fixed1,                 0,
  204.                                              fixed1+kFix3Quarters,     fixed1+kFix3Quarters};
  205.                 gxCapRecord    cap;
  206.                 gxRectangle    endRect;
  207.  
  208.                 *pShape = NewShape4 (gxLineType, fixed1, 0, 0, fixed1);
  209.                 GXSetShapePen (*pShape, IntToFixed (8));
  210.                 SetShapeCommonColor (*pShape, viridian_light);
  211.  
  212.                 endRect.left   = 0;
  213.                 endRect.top    = kFix3Quarters;
  214.                 endRect.right  = 2 * kFix3Quarters;
  215.                 endRect.bottom = -kFix3Quarters;
  216.                 cap.endCap   = GXNewRectangle (&endRect);
  217.                 cap.startCap = GXNewPolygons ((gxPolygons *) star);
  218.  
  219.                 cap.attributes = 0;
  220.                 GXSetShapeCap (*pShape, &cap);
  221.                 GXDisposeShape (cap.startCap);
  222.                 GXDisposeShape (cap.endCap);
  223.             }
  224.             break;
  225.  
  226.         case kDashedOvalPath:
  227.             {
  228.                 gxDashRecord    dashRec;
  229.  
  230.                 bounds.left        =
  231.                 bounds.top        = 0;
  232.                 bounds.right    =
  233.                 bounds.bottom    = kQuarterInch;
  234.  
  235.                 dashRec.attributes = gxBreakDash + gxAutoAdvanceDash;
  236.                 dashRec.dash    = NewOval (&bounds);
  237.                 dashRec.advance    = kHalfInch;
  238.                 dashRec.phase    = 0;
  239.                 dashRec.scale    = kQuarterInch;
  240.  
  241.                 bounds.left        =
  242.                 bounds.top        = 0;
  243.                 bounds.right    =
  244.                 bounds.bottom    = kHalfInch + kEigthInch;
  245.                 *pShape = NewOval (&bounds);
  246.  
  247.                 GXSetShapeDash (*pShape, &dashRec);
  248.                 GXSetShapeFill (*pShape, gxClosedFrameFill);
  249.                 GXSetShapeStyleAttributes (*pShape, gxOutsideFrameStyle);
  250.                 GXSetShapePen (*pShape, kQuarterInch);
  251.                 SetShapeCommonColor (*pShape, light + apple_yellow);
  252.                 GXDisposeShape (dashRec.dash);
  253.             }
  254.             break;
  255.  
  256.         case kJoinPath:
  257.             {
  258.                 fixed    spokes[] = {1L, 8L, 0x55000000,                
  259.                                     0,   IntToFixed (4),
  260.                                     IntToFixed (4), IntToFixed (4),
  261.                                     IntToFixed (4), 0,
  262.                                     IntToFixed (4), IntToFixed (4),
  263.                                     IntToFixed (8), IntToFixed (4),
  264.                                     IntToFixed (4), IntToFixed (4),
  265.                                     IntToFixed (4), IntToFixed (8),
  266.                                     IntToFixed (4), IntToFixed (4)};
  267.                 *pShape    = GXNewPaths ((gxPaths *) spokes);
  268.                 SetShapeCommonColor (*pShape, light + orange);
  269.             }
  270.             break;
  271.         }
  272.  
  273.         if (*pShape)
  274.         {
  275.             GXGetShapeBounds (*pBoxSet, shapeNum + 1, &bounds);
  276.  
  277.             if (shapeNum == kWordGlyphs)
  278.                 GXMoveShapeTo (*pShape, bounds.left + kEigthInch, bounds.bottom - kHalfInch - kEigthInch);
  279.             else if (shapeNum == kArchCurve)
  280.                 CenterShape (*pShape, &bounds);
  281.             else
  282.                 GXSetShapeBounds (*pShape, &bounds);
  283.  
  284.             if (shapeNum == kTrapizoidPolygon  ||
  285.                 shapeNum == kOvalPath  ||
  286.                 shapeNum == kPatternedPolygon  ||
  287.                 shapeNum == kDashedOvalPath)
  288.                 GXInsetShape (*pShape, kQuarterInch);
  289.  
  290.             if (shapeNum == kArrowPolygon)
  291.                 ScaleShapeAboutCenter (*pShape, kFixOneHalf + (kFixOneHalf >> 1), kFixOneHalf + (kFixOneHalf >> 1));
  292.  
  293.             if (shapeNum == kCapsLine)
  294.                 ScaleShapeAboutCenter (*pShape, kFixOneHalf, kFixOneHalf);
  295.  
  296.             if (shapeNum == kJoinPath)
  297.                 ScaleShapeAboutCenter (*pShape, 7 * (kFixOneHalf >> 2), 7 * (kFixOneHalf >> 2));
  298.  
  299.             GXCacheShape (*pShape);
  300.         }
  301.     }
  302.  
  303.     return (true);
  304. }
  305.  
  306.  
  307.     void
  308. DisposeHitTestShapes (gxShape **h1stHitShape, gxShape *pBoxes)
  309. {
  310.     register
  311.     gxShape    *pShape;
  312.     long    shapeCount;
  313.  
  314.     if (pBoxes  &&  *pBoxes)
  315.     {
  316.         if (h1stHitShape  &&  *h1stHitShape)
  317.         {
  318.             shapeCount = GXCountShapeContours (*pBoxes);
  319.             pShape = *h1stHitShape + shapeCount - 1;
  320.  
  321.             while (shapeCount--)
  322.                 DisposeShapeAt (pShape--);
  323.  
  324.             DisposPtr ((Ptr) *h1stHitShape);
  325.             *h1stHitShape = nil;
  326.         }
  327.  
  328.         DisposeShapeAt (pBoxes);
  329.     }
  330. }
  331.